import Vue from "vue";
import VueStorage from "vue-ls";
import settings from "@/core/config/settings";

Vue.use(VueStorage, settings.storage);

/**
 * User
 */
export const UserKey = {
    Token: 'Token',
    RefreshToken: 'RefreshToken',
    UserInfo: 'UserInfo',
    MerchantId: 'MerchantId',
    Routes: 'Routes',
    AllMenu: 'AllMenu',
    Role: 'Role',
    Menu: 'RoleMenu',
};
export const setRoleInfo = (role: string) => Vue.ls.set(UserKey.Role, role);
export const getRoleInfo = (): any => Vue.ls.get(UserKey.Role);
export const removeRoleInfo = () => Vue.ls.remove(UserKey.Role);
//
export const setMenus = (menus: string) => Vue.ls.set(UserKey.Menu, menus);
export const getMenus = () => {
    if (!Vue.ls.get(UserKey.Menu)) {
        return [];
    }
    return Vue.ls.get(UserKey.Menu)
};
export const removeMenus = () => Vue.ls.remove(UserKey.Menu);
//
export const setToken = (token: string) => Vue.ls.set(UserKey.Token, token);
export const getToken = () => Vue.ls.get(UserKey.Token);
export const removeToken = () => Vue.ls.remove(UserKey.Token);
//
export const setRefreshToken = (refresh_token: string) => Vue.ls.set(UserKey.RefreshToken, refresh_token);
export const getRefreshToken = () => Vue.ls.get(UserKey.RefreshToken);
export const removeRefreshToken = () => Vue.ls.remove(UserKey.RefreshToken);
//
export const setUserInfo = (user_info: string) => Vue.ls.set(UserKey.UserInfo, user_info);
export const getUserInfo = () => Vue.ls.get(UserKey.UserInfo) as string;
export const removeUserInfo = () => Vue.ls.get(UserKey.UserInfo);
//
export const setMerchantId = (merchantId: string) => Vue.ls.set(UserKey.MerchantId, merchantId);
export const getMerchantId = () => {
    if (!Vue.ls.get(UserKey.MerchantId)) {
        return "0";
    }
    return Vue.ls.get(UserKey.MerchantId);
};
//
export const removeAllCache = () => {
    for (let key in UserKey) {
        Vue.ls.remove((UserKey as any)[key])
    }
};


/**
 * Dict
 */
export const DictKey = {
    OpenErrorLog: 'OpenErrorLog',
    OpenWarnLog: 'OpenWarnLog',
    HtmlVersion: 'HtmlVersion',
    ApiVersion: 'ApiVersion',
    BaseImageUrl: 'BaseImageUrl',
    SocketUrl: 'SocketUrl',
    showLog: 'showLog',
};
export const setShowLog = (isSHow: any) => {
    if (isSHow) {
        Vue.ls.set(DictKey.showLog, isSHow.toString());
    } else {
        Vue.ls.set(DictKey.showLog, "false");
    }
};
export const showLog = (): any => Vue.ls.get(DictKey.showLog);
export const removeShowLog = () => Vue.ls.remove(DictKey.showLog);
//
export const setImageUrl = (baseImageUrl: string) => Vue.ls.set(DictKey.BaseImageUrl, baseImageUrl);
export const getImageUrl = (): any => Vue.ls.get(DictKey.BaseImageUrl);
export const removeImageUrl = () => Vue.ls.remove(DictKey.BaseImageUrl);
//
export const setSocketUrl = (socketUrl: string) => Vue.ls.set(DictKey.SocketUrl, socketUrl);
export const getSocketUrl = (): any => Vue.ls.get(DictKey.SocketUrl);
export const removeSocketUrl = () => Vue.ls.remove(DictKey.SocketUrl);
//
export const setHtmlVersion = (version: string) => Vue.ls.set(DictKey.HtmlVersion, version);
export const getHtmlVersion = () => {
    if (Vue.ls.get(DictKey.HtmlVersion)) {
        return Vue.ls.get(DictKey.HtmlVersion);
    } else {
        return "0";
    }
};
//
export const setOpenErrorLog = (value: string) => Vue.ls.set(DictKey.OpenErrorLog, value);
export const getOpenErrorLog = () => {
    let item = Vue.ls.get(DictKey.OpenErrorLog);
    if (item) {
        return item == "true";
    }
    return false;
};
//
export const setOpenWarnLog = (value: string) => Vue.ls.set(DictKey.OpenWarnLog, value);
export const getOpenWarnLog = () => {
    let item = Vue.ls.get(DictKey.OpenWarnLog);
    if (item) {
        return item == "true";
    }
    return false;
};
//
export const setApiVersion = (version: string) => Vue.ls.set(DictKey.ApiVersion, version);
export const getApiVersion = () => {
    if (Vue.ls.get(DictKey.ApiVersion)) {
        return Vue.ls.get(DictKey.ApiVersion);
    } else {
        return "0";
    }
};
export const removeHtmlVersion = () => Vue.ls.remove(DictKey.HtmlVersion);

/**
 *App
 */
export const AppKey = {
    sidebarStatus: 'sidebarStatus',
    language: 'language',
    size: 'size',
};

export const getSidebarStatus = () => Vue.ls.get(AppKey.sidebarStatus);
export const setSidebarStatus = (sidebarStatus: string) => Vue.ls.set(AppKey.sidebarStatus, sidebarStatus);

export const getLanguage = () => Vue.ls.get(AppKey.language);
export const setLanguage = (language: string) => Vue.ls.set(AppKey.language, language);

export const getSize = () => Vue.ls.get(AppKey.size);
export const setSize = (size: string) => Vue.ls.set(AppKey.size, size);
